'\" te
.\" Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved.
.\" Copyright 1989 AT&T
.\" Copyright 2021 Oxide Computer Company
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.Dd March 13, 2021
.Dt CHOWN 2
.Os
.Sh NAME
.Nm chown ,
.Nm lchown ,
.Nm fchown ,
.Nm fchownat
.Nd change owner and group of a file
.Sh SYNOPSIS
.In unistd.h
.In sys/types.h
.Ft int
.Fo chown
.Fa "const char *path"
.Fa "uid_t owner"
.Fa "gid_t group"
.Fc
.Ft int
.Fo lchown
.Fa "const char *path"
.Fa "uid_t owner"
.Fa "gid_t group"
.Fc
.Ft int
.Fo fchown
.Fa "int fildes"
.Fa "uid_t owner"
.Fa "gid_t group"
.Fc
.Ft int
.Fo fchownat
.Fa "int fildes"
.Fa "const char *path"
.Fa "uid_t owner"
.Fa "gid_t group"
.Fa "int flag"
.Fc
.Sh DESCRIPTION
The
.Fn chown
and
.Fn fchown
functions set the owner
.Sy ID
and group
.Sy ID
of the file specified by
.Fa path
or referenced by the open file descriptor
.Fa fildes
to
.Fa owner
and
.Fa group
respectively.
If
.Fa owner
or
.Fa group
is specified as -1,
.Fn chown
and
.Fn fchown
do not change the corresponding
.Sy ID
of the file.
.Pp
The
.Fn lchown
function sets the owner
.Sy ID
and group
.Sy ID
of the named file in the same manner as
.Fn chown ,
unless the named file is a symbolic link.
In this case,
.Fn lchown
changes the ownership of the symbolic link file itself, while
.Fn chown
changes the ownership of the file or directory to which the symbolic link
refers.
.Pp
The
.Fn fchownat
function sets the owner ID and group ID of the named  file
in the same manner as
.Fn chown .
If, however, the
.Fa path
argument is relative
.Po
does not start with a
.Qq /
.Pc ,
the path is resolved relative to the
.Fa fildes
argument rather than the current working directory.
If the
.Fa fildes
argument has the special value
.Dv AT_FDCWD ,
the path resolution reverts back to current working directory relative.
If the
.Fa flag
argument is set to
.Dv AT_SYMLINK_NOFOLLOW ,
the function behaves like
.Fn lchown
with respect to symbolic links.
If the
.Fa path
argument is absolute, the
.Fa fildes
argument is ignored.
If the
.Fa path
argument is a null pointer, the function behaves like
.Fn fchown .
.Pp
If
.Fn chown ,
.Fn lchown ,
.Fn fchown ,
or
.Fn fchownat
is invoked by a process that does not have
.Brq Dv PRIV_FILE_SETID
asserted in its effective set, the set-user-ID and set-group-ID bits of the
file mode,
.Dv S_ISUID
and
.Dv S_ISGID
respectively, are cleared
.Po
see
.Xr chmod 2
.Pc .
Additional restrictions apply when changing the ownership to uid 0.
.Pp
The operating system defines several privileges to override restrictions on the
.Fn chown
family of functions.
When the
.Brq Dv PRIV_FILE_CHOWN
privilege is asserted in the effective set of the current process, there are no
restrictions except in the special circumstances of changing ownership to or
from uid 0.
When the
.Brq Dv PRIV_FILE_CHOWN_SELF
privilege is asserted, ownership changes are restricted to the files of which
the ownership matches the effective user ID of the current process.
If neither privilege is asserted in the effective set of the calling process,
ownership changes are limited to changes of the group of the file to the list of
supplementary group IDs and the effective group ID.
.Pp
The operating system provides a configuration option,
.Brq Dv _POSIX_CHOWN_RESTRICTED ,
to control the default behavior of processes and the behavior of the NFS server.
If
.Brq Dv B_POSIX_CHOWN_RESTRICTED
is not in effect, the privilege
.Brq PRIV_FILE_CHOWN_SELF
is asserted in the inheritable set of all processes unless overridden by
.Xr policy.conf 5
or
.Xr user_attr 5 .
To set this configuration option, include the following
line in
.Pa /etc/system :
.Bd -literal -offset indent
set rstchown = 1
.Ed
.Pp
To disable this option, include the following line in
.Pa /etc/system :
.Bd -literal -offset indent
set rstchown = 0
.Ed
.Pp
See
.Xr system 5
and
.Xr fpathconf 2 .
.Pp
Upon successful completion,
.Fn chown ,
.Fn fchown ,
.Fn lchown ,
and
.Fn fchownat
mark for update the
.Fa st_ctime
field of the file.
.Sh RETURN VALUES
Upon successful completion,
.Sy 0
is returned.
Otherwise,
.Sy -1
is returned, the owner and group of the named file remain unchanged, and
.Va errno
is set to indicate the error.
.Sh ERRORS
All of these functions will fail if:
.Bl -tag -width Er
.It Er EPERM
The effective user ID does not match the owner of the file and the
.Brq Dv PRIV_FILE_CHOWN
privilege is not asserted in the effective set of the calling process, or the
.Brq Dv PRIV_FILE_CHOWN_SELF
privilege is not asserted in the effective set of the calling process.
.El
.Pp
The
.Fn chown ,
.Fn lchown ,
and
.Fn fchownat
functions will fail if:
.Bl -tag -width Er
.It Er EACCES
Search permission is denied on a component of the path prefix of
.Fa path .
.It Er EFAULT
The
.Fa path
argument points to an illegal address and for
.Fn fchownat ,
the file descriptor has the value
.Dv AT_FDCWD .
.It Er EINTR
A signal was caught during the execution of the
.Fn chown
or
.Fn lchown
function.
.It Er EINVAL
The
.Fa group
or
.Fa owner
argument is out of range.
.It Er EIO
An I/O error occurred while reading from or writing to the file system.
.It Er ELOOP
Too many symbolic links were encountered in translating
.Fa path .
.It Er ENAMETOOLONG
The length of the
.Fa path
argument exceeds
.Brq Dv PATH_MAX ,
or the length of a
.Fa path
component exceeds
.Brq Dv NAME_MAX
while
.Brq Dv _POSIX_NO_TRUNC
is in effect.
.It Er ENOLINK
The
.Fa path
argument points to a remote machine and the link to that machine is no longer
active.
.It Er ENOENT
Either a component of the path prefix or the file referred to by
.Fa path
does not exist or is a null pathname.
.It Er ENOTDIR
A component of the path prefix of
.Fa path
is not a directory, or the path supplied to
.Fn fchownat
is relative and the file descriptor provided does not refer to a valid
directory.
.It Er EROFS
The named file resides on a read-only file system.
.El
.Pp
The
.Fn chown
and
.Fn fchownat
functions will fail if:
.Bl -tag -width Er
.It Er EBADF
For
.Fn fchown ,
the
.Fa fildes
argument is not an open file descriptor.
.Pp
For
.Fn fchownat ,
the
.Fa path
argument is not absolute and the fildes argument is neither
.Dv AT_FDCWD ,
nor an open file descriptor.
.It Er EIO
An I/O error occurred while reading from or writing to the file system.
.It Er EINTR
A signal was caught during execution of the function.
.It Er ENOLINK
The
.Fa fildes
argument points to a remote machine and the link to that machine is no longer
active.
.It Er EINVAL
The
.Fa group
or
.Fa owner
argument is out of range.
.It Er EROFS
The named file referred to by
.Fa fildes
resides on a read-only file system.
.El
.Sh INTERFACE STABILITY
.Sy Committed
.Sh MT-LEVEL
.Sy Async-Signal-Safe
.Sh SEE ALSO
.Xr chgrp 1 ,
.Xr chown 1 ,
.Xr chmod 2 ,
.Xr fpathconf 2 ,
.Xr system 5 ,
.Xr attributes 7 ,
.Xr standards 7
